package io.renren.modules.groupinfo.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.renren.modules.welderinfo.service.impl.WelderinfoServiceImpl;
import io.renren.modules.weldingmachineinfo.entity.WeldingmachineinfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.groupinfo.dao.GroupinfoDao;
import io.renren.modules.groupinfo.entity.GroupinfoEntity;
import io.renren.modules.groupinfo.service.GroupinfoService;
import org.springframework.web.client.RestTemplate;


@Service("groupinfoService")
public class GroupinfoServiceImpl extends ServiceImpl<GroupinfoDao, GroupinfoEntity> implements GroupinfoService {

    @Autowired
    GroupinfoDao groupinfoDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GroupinfoEntity> page = this.page(
                new Query<GroupinfoEntity>().getPage(params),
                new QueryWrapper<GroupinfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<GroupinfoEntity> getgroupinfo(String groupName, String leaderName) {
        return groupinfoDao.getgroupinfo(groupName,leaderName);
    }

    //1.获取token
    public Object getWmToken() throws JsonProcessingException {
        RestTemplate restTemplate = new RestTemplate();

        // 获取token
//        UsersServiceImpl.ReHeader reHeader = new UsersServiceImpl.ReHeader();
        WelderinfoServiceImpl.ReHeader reHeader = new WelderinfoServiceImpl.ReHeader();
        reHeader.setKey("cmhi2023");
        String url = "http://10.142.3.129:8080/getOpenApiToken";
        String result = restTemplate.postForObject(url,reHeader, String.class);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(result);
        String token = String.valueOf(jsonNode.get("data").get("token")).replace("\"","");
        return token.toString();
    }

    //2.获取焊机信息
//获取焊工信息
//    @Async("screenTaskExecutor")
//    @Scheduled(cron = "0/5 * * * * ?")
    public String getGroupInfo() throws InterruptedException, JsonProcessingException, URISyntaxException {
        String url = "http://10.142.3.129:8080/system/groupinfo/list";
        String token=getWmToken().toString();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        RestTemplate restTemplate = new RestTemplate(requestFactory);

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setBearerAuth(token);
        RequestEntity<Void> requestEntity = new RequestEntity<>(httpHeaders, HttpMethod.GET,new URI(url));
        ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
        String weldmachineinfo = responseEntity.getBody();
//        System.out.println(weldmachineinfo);
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(weldmachineinfo);
            JsonNode rowsNode = jsonNode.get("rows");
            if(!jsonNode.isArray()){
                for (JsonNode rowNode:rowsNode) {
                    Long groupid = Long.valueOf(rowNode.get("groupId").asText());
                    String groupname = rowNode.get("groupName").asText();
                    String leadername = rowNode.get("groupLeader").asText();
                    String grouplocation = rowNode.get("groupPosition").asText();
                    String groupdescribe = rowNode.get("groupComment").asText();
//                    String colmodel = rowNode.get("remark").asText();
                    System.out.println("groupid:"+groupid);
                    System.out.println("groupname:"+groupname);
                    System.out.println("leadername:"+leadername);
                    System.out.println("grouplocation:"+grouplocation);
                    System.out.println("groupdescribe:"+groupdescribe);
                    System.out.println("------------------");
                    GroupinfoEntity groupinfoEntity = new GroupinfoEntity();
                    groupinfoEntity.setGroupid(groupid);
                    groupinfoEntity.setGroupname(groupname);
                    groupinfoEntity.setGrouplocation(grouplocation);
                    groupinfoEntity.setLeadername(leadername);
                    groupinfoEntity.setGroupdescribe(groupdescribe);
                    saveMysql(groupinfoEntity);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return responseEntity.toString();
    }

    public void saveMysql(GroupinfoEntity groupinfoEntity){
        Long groupid = groupinfoEntity.getGroupid();
        int count = groupinfoDao.countByGroupId(groupid);
        if(count>0){
            System.out.println("已经存在");
        } else {
            groupinfoDao.addGroup(groupinfoEntity);
        }
        System.out.println("save");
//        log.info("save");
    }
    //3.将焊机信息存入数据库

}